//
//  MJAnswerView.m
//  MJ
//
//  Created by lina on 2017/12/11.
//  Copyright © 2017年 lina. All rights reserved.
//

#import "MJAnswerView.h"
#import "MJQuestionsModel.h"

@interface MJAnswerView ()
@property (nonatomic, weak) UIView *view;
@property (nonatomic, assign, readwrite) MJAnswerViewStyle style;
@property (nonatomic, strong, readwrite) NSArray <Choise *> *answerArray;
@property (nonatomic, copy, readwrite) NSString *selectAnswer;
@property (nonatomic, copy, readwrite) NSString *qrnum;
/** sel */
@property (nonatomic, strong) UIButton *selectedButton;

@end

@implementation MJAnswerView

- (UIView *)view
{
    if (!_view) {
        UIView *view = [[UIView alloc] init];
        [self addSubview:view];
        _view = view;
    }
    
    return _view;
}

- (instancetype)init
{
    if (self = [super init]) {
        
        self.backgroundColor = HEXColor(@"#f2f2f2");

    }
    
    return self;
}

- (void)reloadViewWithFrame:(CGRect)frame style:(MJAnswerViewStyle)style answerArray:(NSArray *)answerArray
{
    
    
    self.style = style;
    self.answerArray = answerArray;
    [self removeView];
    [self creatControlWithFrame:frame];
}

- (void)creatControlWithFrame:(CGRect)frame
{
    CGFloat btnH = 25;
    CGFloat padding = 10.f;
    CGFloat labelY = 0;
    UIFont *font = [UIFont systemFontOfSize:14.f];
    
    for (int i = 0; i < _answerArray.count; i++) {
        
        MJButton *button = [MJButton buttonWithType:UIButtonTypeCustom];
        ViewBorderRadius(button, 55/2, 0, [UIColor clearColor]);
        [button setTitle:_answerArray[i].select forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"baidi"] forState:UIControlStateNormal];
        button.choise = _answerArray[i];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [button setBackgroundImage:[UIImage imageNamed:@"huidi"] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor colorWithHexString:@"#8c8c8c"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"#ff8280"] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(labelBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).offset(15);
            make.size.mas_equalTo(CGSizeMake(kScreenWidth-30, 55));
            make.centerX.mas_equalTo(self);
            make.top.mas_equalTo(self).offset(70*i);
            
        }];
    }
    
    self.view.frame = CGRectMake(10, 0, kScreenWidth - 20, labelY - padding);
    frame.size.height = kScreenHeight;
    self.frame = frame;
}



- (void)labelBtnOnClick:(MJButton *)labelBtn
{
    _selectedButton.selected = NO;
    _selectedButton = labelBtn;
    _selectedButton.selected = YES;
    
    if (_delegate && [_delegate respondsToSelector:@selector(questionsAnswerView:didClickOptionButton:)]) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [_delegate questionsAnswerView:self didClickOptionButton:labelBtn];
        });
    }
    labelBtn.backgroundColor = HEXColor(@"fafafa");
}

- (void)selectOptionWithBtn:(UIButton *)btn tag:(int)tag
{
    
}

- (void)setBtnSelectedWithTag:(NSInteger)tag
{
    
}

- (void)removeView
{
    [self.view removeFromSuperview];
    self.view = nil;
}

- (void)setStyle:(MJAnswerViewStyle)style
{
    _style = style;
}


//计算文字的长度
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
}

@end
